package com.dj.mingkong.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.core.env.Environment;

import java.util.Map;

public class SpringBeanUtil implements ApplicationContextAware {
    private static final Logger logger = LoggerFactory.getLogger(SpringBeanUtil.class);
    private static ApplicationContext applicationContext = null;

    public SpringBeanUtil() {
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        logger.info("注入ApplicationContext到SpringBeanUtil:{}", applicationContext);
        if (SpringBeanUtil.applicationContext != null) {
            logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:{}", SpringBeanUtil.applicationContext);
        } else {
            SpringBeanUtil.applicationContext = applicationContext;
        }

    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static <T> T getBean(String name) {
        assertContextInjected();
        return (T) applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> targetClz) {
        assertContextInjected();
        Object beanInstance = null;

        try {
            beanInstance = applicationContext.getBean(targetClz);
        } catch (Exception var3) {
        }

        if (beanInstance == null) {
            String simpleName = targetClz.getSimpleName();
            simpleName = Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1);
            beanInstance = applicationContext.getBean(simpleName);
        }

        if (beanInstance == null) {
            throw new RuntimeException("Component " + targetClz + " can not be found in Spring Container");
        } else {
            return (T) beanInstance;
        }
    }

    public static void publishEvent(ApplicationEvent event) {
        applicationContext.publishEvent(event);
    }

    public static void publishEvent(Object event) {
        applicationContext.publishEvent(event);
    }

    public static <T> Map<String, T> getBeans(Class<T> clazz) {
        assertContextInjected();
        return applicationContext.getBeansOfType(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        assertContextInjected();
        return applicationContext.getBean(name, clazz);
    }

    public static Environment getEnvironment() {
        return applicationContext.getEnvironment();
    }

    public static String getEnvironmentProperty(String key) {
        return applicationContext.getEnvironment().getProperty(key);
    }

    public void destroy() {
        clear();
    }

    public static void clear() {
        logger.debug("清除SpringBeanUtil中的ApplicationContext:{}", applicationContext);
        applicationContext = null;
    }

    private static void assertContextInjected() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicaitonContext未注入,请在spring.xml或注解中定义SpringBeanUtil");
        }
    }
}
